package com.zzyl.nursing.service.impl;

import java.util.*;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IWeChatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人家属Service业务层处理
 *
 * @author yjb
 * @date 2025-10-16
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private IWeChatService weChatService;
    @Autowired
    private TokenService tokenService;

    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //获取openid
        String openid = weChatService.getopenid(dto.getCode());
        //根据openid查询家属
        LambdaQueryWrapper<FamilyMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FamilyMember::getOpenId, openid);
        FamilyMember familyMember = this.getOne(queryWrapper);
        //获取手机号
        //    String phone = weChatService.getPhone(dto.getPhoneCode());
        String phoneCode = dto.getPhoneCode();
        String phone = weChatService.getphone(phoneCode);
        //查不到新增
//        if (familyMember == null) {
//            familyMember = new FamilyMember();
//
//            familyMember.setPhone(phone);
//            String nickName = DEFAULT_NICKNAME_PREFIX.get(new Random().nextInt(DEFAULT_NICKNAME_PREFIX.size() + 1))+phone.substring(7);
//            familyMember.setName(nickName);//规则 随机产生名字拼接手机号后四位
//            familyMember.setOpenId(openid);
//            this.save(familyMember);
//        }else{
//            //修改
//            if (!familyMember.getPhone().equals(phone)){
//                familyMember.setPhone(phone);
//                this.updateById(familyMember);
//            }
//        }
        if (familyMember == null) {
            familyMember = new FamilyMember();
            familyMember.setPhone(phone);
            //从DEFAULT_NICKNAME_PREFIX随机挑选一个 + 手机号的后四位 0-8
            String nicknamePrefix = "用户"; // 默认前缀
            familyMember.setName(DEFAULT_NICKNAME_PREFIX.get(new Random().nextInt(DEFAULT_NICKNAME_PREFIX.size() + 1)) + phone.substring(7));
            familyMember.setOpenId(openid);
            familyMemberMapper.insert(familyMember);
        } else {
            // 这个用户是否修改过手机号
            if (!StrUtil.equals(familyMember.getPhone(), phone)) {
                familyMember.setPhone(phone);
            }
            familyMemberMapper.updateById(familyMember);

        }


        //生成token
        Map<String, Object> map = new HashMap();
        map.put("id", familyMember.getId());
        map.put("nickName", familyMember.getName());
        String token = tokenService.createToken(map);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(familyMember.getName());

        return loginVo;
    }

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of("生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );
}
